package game.view.mediator 
{
	import game.constants.ApplicationNotification;
	import game.model.building.BuildingProxy;
	import game.model.building.vo.BuildingVO;
	import game.model.texture.TextureProxy;
	import game.view.event.BuildingEvent;
	import game.view.screen.components.BuildingComponent;
	import game.view.screen.GameBoardScreen;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	/**
	 * ...
	 * @author Thot
	 */
	public class GameBoardMediator extends Mediator implements IMediator
	{
		
		public static const NAME:String = "GameBoardMediator";
		private var _screen:GameBoardScreen;
		
		public function GameBoardMediator( p_board:GameBoardScreen ) 
		{
			_screen = p_board;
			super( NAME, _screen );
			init();
		}
		
		private function init():void 
		{
			_screen.init();
			_screen.addEventListener( BuildingEvent.TOUCH_BUILDING, touchBuildingHandler );
		}
		
		private function touchBuildingHandler(e:BuildingEvent):void 
		{
			sendNotification( ApplicationNotification.START_EVOLUTION, { "name": e.buildingName } );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ 
						ApplicationNotification.ADD_BUILDING,
						ApplicationNotification.START_EVOLUTION,
						ApplicationNotification.UPDATE_DATA,
			];
		}
		
		
		override public function handleNotification (notification:INotification) : void
		{
			var buildingProxy:BuildingProxy = facade.retrieveProxy( BuildingProxy.NAME ) as BuildingProxy;
			var texProxy:TextureProxy = facade.retrieveProxy( TextureProxy.NAME ) as TextureProxy;
			var component:BuildingComponent = null;
			var list:Vector.<BuildingVO> = null;
			var vo:BuildingVO;
			var i:int = 0;
			
			switch( notification.getName() )
			{
				case ApplicationNotification.ADD_BUILDING:
					component = new BuildingComponent();
					component.name = notification.getBody().name;
					component.init( texProxy.getTextureByName("batiment1").data );
					_screen.addBuilding( component );
					break;
					
				case ApplicationNotification.UPDATE_DATA:
					list = buildingProxy.getBuildings();
					i = list.length;
					
					while ( --i > -1 )
					{
						vo = list[i];
						_screen.actualizeBuildingState( vo );
					}
					
					break;
					
				case ApplicationNotification.START_EVOLUTION:
					break;
			}
		}
		
		
	}

}